package com.icesoft.xsnow.mq.provider;

import org.apache.commons.lang3.RandomUtils;
import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: xsnow
 * @description: 消息生产者
 * @author: xuefeng.gao
 * @create: 2019-07-23 12:22
 **/
@SpringBootApplication
@EnableBinding({ Source.class})
public class XsnowMqProviderApplication implements CommandLineRunner{

    public static void main(String[] args) {
        SpringApplication.run(XsnowMqProviderApplication.class, args);
    }

    @Autowired
    private MessageChannel output;

    @Override
    public void run(String... args) throws Exception {
        while(true){
            Thread.sleep(1000);
            Map<String, Object> headers = new HashMap();
            headers.put(MessageConst.PROPERTY_TAGS, "tagStr"+ RandomUtils.nextInt(1,3));
            Message msg = MessageBuilder.withPayload("Hello RMQ"+RandomUtils.nextLong()).copyHeaders(headers).build();
            output.send(msg);
        }
    }
}
